package com.example.addwater.web;

import com.example.addwater.service.WaterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ChumML created by 2020/12/02 8:54
 */
@Api(tags = "水印")
@RestController
public class WaterController {

    final WaterService waterService;

    public WaterController(WaterService waterService) {this.waterService = waterService;}


    @ApiOperation(value = "背景水印", produces = "application/octet-stream")
    @PostMapping("textWater")
    public void addImageWater(MultipartFile image, HttpServletResponse response) throws IOException {
        InputStream inputStream = waterService.addTextMarkWater ( image );
        response.setContentType ( "image/jpeg;image/png;" );
        response.addHeader ( "Content-Disposition", "attachment;filename=" + image.getOriginalFilename () );
        IOUtils.copy ( inputStream, response.getOutputStream () );
    }

    @ApiOperation(value = "前置水印", produces = "application/octet-stream")
    @PostMapping("imageWater")
    public void addTextWater(MultipartFile image, HttpServletResponse response) throws Exception {
        InputStream inputStream = waterService.addImageAndTextMarkWater ( image, "用户名", "北京市海淀区振兴小学" );
        response.setContentType ( "image/jpeg;image/png;" );
        response.addHeader ( "Content-Disposition", "attachment;filename=" + image.getOriginalFilename () );
        IOUtils.copy ( inputStream, response.getOutputStream () );

    }
}
